package api

import (
	"bbs-go/internal/models"
	"bbs-go/internal/services"
	"strconv"

	"github.com/kataras/iris/v12"
	"github.com/mlogclub/simple/web"
	"github.com/mlogclub/simple/web/params"
)

type SystemNoticeController struct {
	Ctx iris.Context
}

func (c *SystemNoticeController) GetBy(id int64) *web.JsonResult {
	t := services.SystemNoticeService.GetById(id)
	if t == nil {
		return web.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
	}

	res := models.SystemNoticeSimple{
		Id:          t.Id,
		CreatedAt:   t.CreatedAt.Format("2006-01-02 15:04:05"),
		Title:       t.Title,
		Summary:     t.Summary,
		ContentType: t.ContentType,
		Content:     t.Content,
		Cover:       t.Cover,
	}
	return web.JsonData(res)
}

func (c *SystemNoticeController) AnyList() *web.JsonResult {
	p := params.NewQueryParams(c.Ctx).PageByReq()
	p.Gte("status", 2)
	list, paging := services.SystemNoticeService.FindPageByParams(p.Desc("id"))
	resList := []models.SystemNoticeSimple{}
	for _, v := range list {
		resList = append(resList, models.SystemNoticeSimple{
			Id:          v.Id,
			CreatedAt:   v.CreatedAt.Format("2006-01-02 15:04:05"),
			Title:       v.Title,
			Summary:     v.Summary,
			ContentType: v.ContentType,
			Content:     v.Content,
			Cover:       v.Cover,
		})
	}
	return web.JsonData(&web.PageResult{Results: resList, Page: paging})
}
